package cn.dsttl3.tupan.Ability.slice;

import cn.dsttl3.tupan.ResourceTable;
import cn.dsttl3.tupan.utils.DataUtils;
import cn.dsttl3.tupan.utils.aria2.Aria2;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.preferences.Preferences;

public class Aria2AbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_aria2);
        Preferences preferences = new DataUtils().getPreferences(getApplicationContext());
        TextField tfIP = (TextField) findComponentById(ResourceTable.Id_tf_aria2_ip);
        TextField tfPort = (TextField) findComponentById(ResourceTable.Id_tf_aria2_port);
        Button btnTest = (Button) findComponentById(ResourceTable.Id_btn_aria2_test);
        Text textIP = (Text) findComponentById(ResourceTable.Id_tx_aria2_ip);
        Text textPort = (Text) findComponentById(ResourceTable.Id_tx_aria2_port);
        Text textMsg = (Text) findComponentById(ResourceTable.Id_tx_test_msg);
        textIP.setText("当前IP为：" + preferences.getString("aria2_ip",null));
        textPort.setText("当前端口为：" + preferences.getString("aria2_port","6800"));
        btnTest.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                btnTest.setClickable(false);
                textMsg.setText("正在测试");
                String ip = tfIP.getText();
                int port = Integer.getInteger(tfPort.getText(),6800);
                boolean b1 = ip.matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
                if (ip.length() > 0 && b1 && port > 0 && port < 65535){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (new Aria2().test(ip,String.valueOf(port))){
                                getUITaskDispatcher().asyncDispatch(new Runnable() {
                                    @Override
                                    public void run() {
                                        new ToastDialog(getContext()).setText("测试通过").show();
                                        preferences.putString("aria2_ip",ip);
                                        preferences.putString("aria2_port",String.valueOf(port));
                                        preferences.flush();
                                        terminateAbility();
                                    }
                                });
                            }else {
                                getUITaskDispatcher().asyncDispatch(new Runnable() {
                                    @Override
                                    public void run() {
                                        textMsg.setText("测试失败");
                                        btnTest.setClickable(true);
                                    }
                                });
                            }
                        }
                    }).start();



                }else {
                    textMsg.setText("填写错误");
                    btnTest.setClickable(true);
                }
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
